package com.bluesmart.mia;

import java.util.Locale;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.city.CityPicker;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class GeneralActivity extends BaseActivity implements OnClickListener{
	JSONObject mJson = null;
	String mLang;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_general);

		((TextView) findViewById(R.id.center)).setText(getString(R.string.general));
		((Button) findViewById(R.id.right)).setCompoundDrawablesWithIntrinsicBounds(0, 0,
				R.drawable.a01, 0);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.setting0).setOnClickListener(this);
		findViewById(R.id.setting1).setOnClickListener(this);
		findViewById(R.id.setting2).setOnClickListener(this);
		findViewById(R.id.setting3).setOnClickListener(this);
//		findViewById(R.id.setting4).setOnClickListener(this);
//		findViewById(R.id.setting5).setOnClickListener(this);
		findViewById(R.id.text0).setOnClickListener(this);
		
		sendHttpRequest();
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				mJson = JSON.parseObject(response);
				if(mJson != null)
				{
					((TextView)findViewById(R.id.text0)).setText(mJson.getString("location"));
					
					mLang = mJson.getString("language");
					
					if(mJson.getString("language").equals("1")) ((Button)findViewById(R.id.setting0)).setSelected(true);
					else ((Button)findViewById(R.id.setting1)).setSelected(true);
					
					if(((DemoApp) getApplicationContext()).glbUnit == 1) ((Button)findViewById(R.id.setting2)).setSelected(true);
					else ((Button)findViewById(R.id.setting3)).setSelected(true);
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("handType", "get");

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/GetMiaConfig.ashx", JSON.toJSONString(params), loginHandler);
	}

	@Override
	public void onClick(View arg0) {
		switch(arg0.getId())
		{
		case R.id.text0:
			showQuerySearch();
			break;
		
		case R.id.setting0:
			((Button)findViewById(R.id.setting0)).setSelected(true);
			((Button)findViewById(R.id.setting1)).setSelected(false);
			break;
			
		case R.id.setting1:
			((Button)findViewById(R.id.setting0)).setSelected(false);
			((Button)findViewById(R.id.setting1)).setSelected(true);
			break;
			
		case R.id.setting2:
			((Button)findViewById(R.id.setting2)).setSelected(true);
			((Button)findViewById(R.id.setting3)).setSelected(false);
			break;
			
		case R.id.setting3:
			((Button)findViewById(R.id.setting2)).setSelected(false);
			((Button)findViewById(R.id.setting3)).setSelected(true);
			break;
			/*
		case R.id.setting4:
			((Button)findViewById(R.id.setting4)).setSelected(true);
			((Button)findViewById(R.id.setting5)).setSelected(false);
			break;
			
		case R.id.setting5:
			((Button)findViewById(R.id.setting4)).setSelected(false);
			((Button)findViewById(R.id.setting5)).setSelected(true);
			break;
			*/
		case R.id.left:
			finish();
			break;
			
		case R.id.right:
			sendSetHttpRequest();
			break;
		}
	}
	
	void showQuerySearch() {
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_city);
		mDialog.findViewById(R.id.cancel).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
					}
				});
		mDialog.findViewById(R.id.ok).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						CityPicker picker = (CityPicker)mDialog.findViewById(R.id.citypicker);
						((TextView)findViewById(R.id.text0)).setText(picker.getAddress());
						mDialog.dismiss();
					}
				});
		mDialog.show();
	}
	
	private void sendSetHttpRequest() {
		if(mJson == null)
		{
			ToastUtil.showToastWithAlertPic(R.string.error_3000);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					ToastUtil.showToastWithAlertPic(R.string.success);
					
					String lang = "2";
					if(((Button)findViewById(R.id.setting0)).isSelected()) lang = "1";
					if(!mLang.equals(lang))
					{
						if(lang.equals("1"))
						{
							Locale.setDefault(Locale.ENGLISH); 
							Configuration config = getBaseContext().getResources().getConfiguration(); 
							config.locale = Locale.ENGLISH; 
							getBaseContext().getResources().updateConfiguration(config
									, getBaseContext().getResources().getDisplayMetrics());
						}
						else
						{
							Locale.setDefault(Locale.CHINESE); 
							Configuration config = getBaseContext().getResources().getConfiguration(); 
							config.locale = Locale.CHINESE; 
							getBaseContext().getResources().updateConfiguration(config
									, getBaseContext().getResources().getDisplayMetrics());
						}	

						ToastUtil.showToastWithAlertPic(R.string.change_lan);
					}
					
					((DemoApp) getApplicationContext()).glbUnit = 1;
					if(((Button)findViewById(R.id.setting2)).isSelected()) PreferencesUtils.putInt(GeneralActivity.this, "unithabit", 1);
					else 
					{
						((DemoApp) getApplicationContext()).glbUnit = 2;
						PreferencesUtils.putInt(GeneralActivity.this, "unithabit", 2);
					}
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else 
				{
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		if(((Button)findViewById(R.id.setting0)).isSelected()) params.put("language", "1");
		else params.put("language", "2");
		if(((Button)findViewById(R.id.setting2)).isSelected()) params.put("units", "1");
		else params.put("unithabit", "2");
		params.put("location", ((TextView)findViewById(R.id.text0)).getText().toString());
//		if(((Button)findViewById(R.id.setting4)).isSelected()) params.put("temperature", "fahrenheit");
//		else params.put("temperature", "celsius");
		params.put("handType", "update");

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/GetMiaConfig.ashx", JSON.toJSONString(params), loginHandler);
	}
}
